﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace OrganizedConfusion.Game_Objects
{
    public class Floor : BaseObject
    {
        public Floor()
        {
            type = BaseObject.OBJ_TYPE.OBJ_FLOOR;
        }

        public void Update(GameTime gametime)
        {
            // Nothing to update, I believe...
        }

        public override bool CheckCollision(BaseObject obj)
        {
            // TODO: Tweak collision and check all cases.
            if (obj.Type != (int)OBJ_TYPE.OBJ_PLAYER)
                return false;

            Player user = (Player)obj;
            Vector2 MyPos = GetCollisionRectPosition();
            Vector2 TheirPos = user.GetCollisionRectPosition();

            if ((TheirPos.X + obj.ImageDimensions.X) > MyPos.X && TheirPos.X < MyPos.X + ImageDimensions.X)
            {
                // Colliding from the top
                if ((TheirPos.Y + obj.ImageDimensions.Y) > MyPos.Y && TheirPos.Y < MyPos.Y)
                {
                    user.Velocity = new Vector2(user.Velocity.X, 0.0f);
                    user.Position = new Vector2(user.Position.X, MyPos.Y - user.Origin.Y);
                    return true;
                }

                if (TheirPos.Y < (MyPos.Y + ImageDimensions.Y) && (TheirPos.Y + obj.ImageDimensions.Y) > (MyPos.Y + ImageDimensions.Y))
                {
                    user.Velocity = new Vector2(user.Velocity.X, 0.0f);
                    user.Position = new Vector2(user.Position.X, MyPos.Y + (ImageDimensions.Y*2));
                    return true;
                }
            }

            return false;
        }

        public override void Render(Microsoft.Xna.Framework.Graphics.SpriteBatch sb)
        {
            sb.Draw(ContentsManager.Instance.Draw(Image), Position, new Rectangle(0,64,128,64), Color.White, 0.0f, Origin, Scale, SpriteEffects.None, 0.0f);
        }
    }
}
